<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Model\Mall;
use App\Http\Services\MsgServices;

class SendGoodToUserCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'SendGoodToUserCommand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '批量给用户发商品';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        do {
            //商品1
            $goodIds_1 = [42];
            //送礼1档用户 20天
            $userIds_1 = [
                21075,104301,41877,41877,104815,104100,104631,104100,106165,86884,106741,106821,106821,34328,107219,21594,100612,100612
            ];
            //送礼2档
            $userIds_2 = [
                
            ];

            
            //商品2
            $goodIds_2 = [38,31,30];
            $userIds_3 = [
                
            ];

            //送礼3挡
            $goodIds_3 = [39,40,41];
            $userIds_4 = [
                
            ];
            //送礼20天商品 42,43,44
            $this->_userSendGood($userIds_1, $goodIds_1, 7);
            $this->_userSendMsg($userIds_1, '用户误充值补发奖励，吉星高照头像框*7天，祝您玩的愉快～');

            // //送7天用户商品 42,43,44
            // $this->_userSendGood($userIds_2, $goodIds_1, 7);
            // $this->_userSendMsg($userIds_1, '恭喜您获得价值10000元装扮大礼包，已成功下发，快去我的装扮里查看吧～');

            // //送礼7天 38,31,30
            // $this->_userSendGood($userIds_3, $goodIds_2, 7);
            // $this->_userSendMsg($userIds_3, '恭喜您获得价值3000钻石的装扮大礼包，已成功下发，快去我的装扮里查看吧～');

            // //送礼7天 39,40,41
            // $this->_userSendGood($userIds_4, $goodIds_3, 7);
            // $this->_userSendMsg($userIds_4, '恭喜您获得价值5000钻石的装扮大礼包，已成功下发，快去我的装扮里查看吧～');
            

        } while (false);
    }
    //用户批量发礼物
    public function _userSendGood($userIds, $goodIds, $day)
    {
        //有效期
        $timeLimit = $day*86400;
        $mall_model = new Mall();

        $goodMap = [];
        foreach($goodIds as $goodId) {
            $goodInfo = $mall_model->getMallGoodByGoodsId($goodId);
            if(empty($goodInfo)) {
                echo '商品不存在'.$goodId;
                exit;
            }
            $skuList = $mall_model->getSkuDataByGoods($goodId);
            if(empty($skuList[0])) {
                echo '商品不存在'.$goodId;
                exit;
            }
            $goodMap[$goodId]['goods_name'] = $goodInfo['goods_name'];
            $goodMap[$goodId]['goods_type'] = $goodInfo['goods_type'];
            $goodMap[$goodId]['sku_id'] = $skuList[0]['sku_id'];
        }
        foreach($userIds as $userId) {
            foreach($goodIds as $goodId) {
                //商品增加
                $mall_model->shoppingAct(
                    11,
                    $userId,
                    $goodId,
                    $goodMap[$goodId]['goods_name'],
                    $goodMap[$goodId]['goods_type'],
                    $goodMap[$goodId]['sku_id'],
                    $timeLimit,
                    0,
                    $timeLimit,
                    time(),
                    2
                );
                echo '商品赠送完成 用户 '.$userId.' 商品 '.$goodId."\n";
            }
        }
    }
    //用户批量发系统消息
    public function _userSendMsg($userIds, $msg)
    {
        $msgServices = new MsgServices();

        foreach($userIds as $userId) {
            $msgServices->send_sys_text_msg($userId, $msg);

            echo '发系统消息完成 用户 '.$userId."\n";
        }
    }
    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }

}